﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Framework.FSM;

namespace BridgeShips
{
    /// <summary>
    /// 桥面展开
    /// </summary>
    public class BridgeExpansion : IFsmState
    {
        #region 用不着的部分
        public void Destroy(IFSM fsm)
        {

        }

        public void Exit(IFSM fsm)
        {
        }

        public void Init(IFSM fsm)
        {

        }

        public void Pause(IFSM fsm)
        {

        }

        public void Reset(IFSM fsm)
        {

        }

        public void Resume(IFSM fsm)
        {

        }
        #endregion

        private FsmStateDataBase m_FsmStateDataBase;
        private GameObject m_ParentObj;
        private bool m_isfinish = false;
        private bool m_isCanChangeParent = false;

        public void Enter(IFSM fsm, FsmStateDataBase data)
        {
            m_FsmStateDataBase = data;
            m_ParentObj = m_FsmStateDataBase.m_Obj;
            m_isfinish = false;
            m_isCanChangeParent = false;
        }
        public void Update(float frameTime, float realTime)
        {
            //设置桥面展开动画，然后设置节点关系
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                if (m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().isBridge)
                {
                    m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().m_isBridgeAnimation = true;
                    if (m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().BridgeAnimationCompleted())
                    {
                        m_isfinish = true;
                    }

                }
                if (m_isfinish)
                {
                    var bridge = m_ParentObj.transform.GetChild(i).Find("ship");
                    var car = m_ParentObj.transform.GetChild(i).Find("Car");
                    bridge.SetParent(car.Find("CarHopper/CarHopper_a/CarHopper_b"));
                    m_isCanChangeParent = true;
                }
            }
            if (m_isCanChangeParent)
            {
                m_FsmStateDataBase.m_CurrentIndex++;
                GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<CarMove>(m_FsmStateDataBase);
                m_isCanChangeParent = false;
            }
            
           
        }
    }
    /// <summary>
    /// 倒车到水边
    /// </summary>
    public class CarMove : IFsmState
    {
        #region 用不到的部分
        public void Destroy(IFSM fsm)
        {
           
        }
        public void Exit(IFSM fsm)
        {
           
        }

        public void Init(IFSM fsm)
        {
           
        }

        public void Pause(IFSM fsm)
        {
           
        }

        public void Reset(IFSM fsm)
        {
           
        }

        public void Resume(IFSM fsm)
        {
           
        }
        #endregion

        private FsmStateDataBase m_FsmStateDataBase;
        private GameObject m_ParentObj;

        public void Enter(IFSM fsm, FsmStateDataBase data)
        {
            m_FsmStateDataBase = data;
            m_ParentObj = m_FsmStateDataBase.m_Obj;
        }
        public void Update(float frameTime, float realTime)
        {
            //初始958.7812f，10.36,709.85  //974.5674,8.4711,710.4
            if (m_ParentObj.transform.GetChild(0).transform.position.x>= 983.5f)
            {
                //for (int i = 0; i < m_ParentObj.transform.childCount; i++)
                //{
                //    m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().m_MoveForce = 0.4f;
                //}
                m_FsmStateDataBase.m_CurrentIndex++;
                GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<CarAnimation>(m_FsmStateDataBase);
                return;
            }
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().CarMove(true);
            }
        }
    }
    /// <summary>
    /// 车斗抬起，车斗后滑
    /// </summary>
    public class CarAnimation : IFsmState
    {
        #region 用不到的部分
        public void Destroy(IFSM fsm)
        {
            
        }

        public void Exit(IFSM fsm)
        {
        }

        public void Init(IFSM fsm)
        {
        }

        public void Pause(IFSM fsm)
        {
        }

        public void Reset(IFSM fsm)
        {
        }

        public void Resume(IFSM fsm)
        {
        }
        #endregion
        private FsmStateDataBase m_FsmStateDataBase;
        private GameObject m_ParentObj;

        public void Enter(IFSM fsm, FsmStateDataBase data)
        {
            m_FsmStateDataBase = data;
            m_ParentObj = m_FsmStateDataBase.m_Obj;
        }
        public void Update(float frameTime, float realTime)
        {
            if (m_ParentObj.transform.GetChild(m_ParentObj.transform.childCount-1).GetComponent<transportCarAnimation>().UpAnimationCompleted())
            {
                m_FsmStateDataBase.m_CurrentIndex++;
                GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<ShipMoveInWater>(m_FsmStateDataBase);
                return;
            }
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().m_IsStartAnimation = true;
            }
        }
    }
    /// <summary>
    /// 舟和桥滑入水中
    /// </summary>
    public class ShipMoveInWater : IFsmState
    {
        #region MyRegion
        public void Destroy(IFSM fsm)
        {
        }

        public void Exit(IFSM fsm)
        {
        }

        public void Init(IFSM fsm)
        {
        }

        public void Pause(IFSM fsm)
        {
        }

        public void Reset(IFSM fsm)
        {
        }

        public void Resume(IFSM fsm)
        {
        }
        #endregion
        private FsmStateDataBase m_FsmStateDataBase;
        private GameObject m_ParentObj;
        private float m_timer = 0;
        private bool m_isFinish = false;
        public void Enter(IFSM fsm, FsmStateDataBase data)
        {
            m_FsmStateDataBase = data;
            m_ParentObj = m_FsmStateDataBase.m_Obj;
            m_isFinish = false;
        }
        public void Update(float frameTime, float realTime)
        {
            
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                var bridge = m_ParentObj.transform.GetChild(i).Find("Car/CarHopper/CarHopper_a/CarHopper_b/ship");
                if (bridge!=null)
                {
                    bridge.SetParent(m_ParentObj.transform.GetChild(i));
                }
                //初始位置：0,2.38f,-1.53f    -----0,2.2f,-2.9f   
                var bridge1 = m_ParentObj.transform.GetChild(i).Find("ship");
                if (bridge1!=null)
                {
                    ShipMoveToWater(bridge1);
                }
                
            }
            if (m_isFinish)
            {
                m_timer += Time.deltaTime;
                if (m_timer > 3f)
                {
                    m_timer = 0;
                    m_FsmStateDataBase.m_CurrentIndex++;
                    GameEntry.Instance.FsmManager.GetFsm("BSAnimation").ChangeState<CarAnimationBack>(m_FsmStateDataBase);
                    m_isFinish = false;
                }
            }
        }
        private void ShipMoveToWater(Transform ship) {
            var point1 = ship.Find("point1").position;
            var point2 = ship.Find("point2").position;
            var point3 = ship.Find("point3").position;
            var point0 = ship.parent.Find("Car/CarHopper/CarHopper_a/CarHopper_b/point0").position;

            Vector3 dir = (ship.position - point1).normalized;//方向
            Vector3 carDir = (ship.position - point0).normalized;
            
            float z = ship.localPosition.z;

            if (z> -2.9f)
            {
                ship.position -= carDir * Time.deltaTime;
                
            }
            else if (z< -2.9f&& z >-9f )
            {
                float shipAngleX = ship.localRotation.eulerAngles.x;
                if (point2.y <= 8f)//如果参考点低于水面，则旋转舟船
                {
                    float rotAngle = Vector3.Angle(dir, Vector3.right);//需要旋转的角度
                    shipAngleX = Time.deltaTime * 5f;
                    ship.localRotation *= Quaternion.Euler(new Vector3(shipAngleX, 0, 0));
                }
                ship.position -= carDir * Time.deltaTime;
            }
            else if(z <-9f &&z > -9.5f)
            {
                float shipAngleX = ship.localRotation.eulerAngles.x;
                if (point2.y <= 7.9f)//如果参考点低于水面，则旋转舟船
                {
                    float rotAngle = Vector3.Angle(dir, Vector3.right);//需要旋转的角度
                    shipAngleX = Time.deltaTime * 5f;
                    ship.localRotation *= Quaternion.Euler(new Vector3(shipAngleX, 0, 0));
                }
                else if ( point2.y >= 8.0f)//如果参考点低于水面，则旋转舟船
                {
                    float rotAngle = Vector3.Angle(dir, Vector3.right);//需要旋转的角度
                    shipAngleX = -Time.deltaTime * 5f;
                    ship.localRotation *= Quaternion.Euler(new Vector3(shipAngleX, 0, 0));
                }
                if (point3.y <= 8.2f)
                {
                    ship.position += Vector3.up * Time.deltaTime * 2f;

                }
                else if (point3.y >= 8.3f)
                {
                    ship.position -= Vector3.up * Time.deltaTime * 2f;
                }
                else
                {
                    m_isFinish = true;
                }
            }
        }
    }
    /// <summary>
    /// 车斗收回,车辆驶离水面
    /// </summary>
    public class CarAnimationBack : IFsmState
    {
        #region MyRegion
        public void Destroy(IFSM fsm)
        {
        }
        public void Exit(IFSM fsm)
        {
        }

        public void Init(IFSM fsm)
        {
        }

        public void Pause(IFSM fsm)
        {
        }

        public void Reset(IFSM fsm)
        {
        }

        public void Resume(IFSM fsm)
        {
        }
        #endregion
        private FsmStateDataBase m_FsmStateDataBase;
        private GameObject m_ParentObj;

        private float amplitude;//振幅
        private float tick;
        private Vector3 offset;
        private Vector3[] m_originPositions;
        private List<Transform> ships = new List<Transform>();
        
        public void Enter(IFSM fsm, FsmStateDataBase data)
        {
            m_FsmStateDataBase = data;
            m_ParentObj = m_FsmStateDataBase.m_Obj;
            amplitude = 2 * Mathf.PI / 10f;
            tick = Random.Range(0, 2 * Mathf.PI);
            offset = new Vector3(0, 0.1f, 0);
            m_originPositions = new Vector3[m_ParentObj.transform.childCount];
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                var ship = m_ParentObj.transform.GetChild(i).Find("ship");
                var pos = ship.position;
                m_originPositions[i] = ship.position;
                ship.SetParent(m_ParentObj.transform.parent);
                ship.position = pos;
                ships.Add(ship);
            }
        }
        public void Update(float frameTime, float realTime)
        {
            for (int i = 0; i < m_ParentObj.transform.childCount; i++)
            {
                if (m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().DownAnimationCompleted())
                {
                    //车辆驶离水面
                    //初始958.7812f，10.36,709.85  //974.5674,8.4711,710.4
                    if (m_ParentObj.transform.GetChild(i).transform.position.x <= 969.8f)
                    {
                       
                        return;
                    }
                    m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().CarMove(false);
                    
                }
                else
                {
                    var ship = ships[i];
                    tick = tick + Time.deltaTime * amplitude;
                    var amp = new Vector3(Mathf.Cos(tick) * offset.x, Mathf.Sin(tick) * offset.y, 0);
                    ship.position = m_originPositions[i] + amp;

                    //车架收回
                    m_ParentObj.transform.GetChild(i).GetComponent<transportCarAnimation>().m_isBackAnimation = true;
                }
            }
        }
    }
}
